package cn.iocoder.yudao.module.wms.controller.admin.location;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wms.controller.admin.location.vo.LocationPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.location.vo.LocationRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.location.vo.LocationSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.location.LocationDO;
import cn.iocoder.yudao.module.wms.service.location.LocationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 货位档案")
@RestController
@RequestMapping("/wms/location")
@Validated
public class LocationController {

    @Resource
    private LocationService locationService;

    @PostMapping("/create")
    @Operation(summary = "创建货位档案")
    @PreAuthorize("@ss.hasPermission('wms:location:create')")
    public CommonResult<Long> createLocation(@Valid @RequestBody LocationSaveReqVO createReqVO) {
        return success(locationService.createLocation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新货位档案")
    @PreAuthorize("@ss.hasPermission('wms:location:update')")
    public CommonResult<Boolean> updateLocation(@Valid @RequestBody LocationSaveReqVO updateReqVO) {
        locationService.updateLocation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除货位档案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:location:delete')")
    public CommonResult<Boolean> deleteLocation(@RequestParam("id") Long id) {
        locationService.deleteLocation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得货位档案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:location:query')")
    public CommonResult<LocationRespVO> getLocation(@RequestParam("id") Long id) {
        LocationDO location = locationService.getLocation(id);
        return success(BeanUtils.toBean(location, LocationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得货位档案分页")
    @PreAuthorize("@ss.hasPermission('wms:location:query')")
    public CommonResult<PageResult<LocationRespVO>> getLocationPage(@Valid LocationPageReqVO pageReqVO) {
        PageResult<LocationDO> pageResult = locationService.getLocationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LocationRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得货位精简列表", description = "主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('wms:location:query')")
    public CommonResult<List<LocationRespVO>> getLocationSimpleList() {
        List<LocationDO> list = locationService.getLocationSimpleList();
        return success(BeanUtils.toBean(list, LocationRespVO.class));
    }

    @GetMapping("/getListByWarehouse")
    @Operation(summary = "获得现存量货位精简列表", description = "主要用于前端的下拉选项")
    @Parameter(name = "inventoryId", description = "存货编号", required = true)
    @Parameter(name = "warehouseId", description = "仓库编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:inventory:query')")
    public CommonResult<List<LocationRespVO>> getListByWarehouse(@RequestParam("inventoryId") Long inventoryId, @RequestParam("warehouseId") String warehouseId) {
        List<LocationDO> list = locationService.getListByWarehouse(inventoryId, warehouseId);
        return success(convertList(list, location -> new LocationRespVO()
                .setId(location.getId()).setName(location.getName())));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出货位档案 Excel")
    @PreAuthorize("@ss.hasPermission('wms:location:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLocationExcel(@Valid LocationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LocationDO> list = locationService.getLocationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "货位档案.xls", "数据", LocationRespVO.class,
                        BeanUtils.toBean(list, LocationRespVO.class));
    }

}